package com.csnf.mall.servermvc.imp;

import com.csnf.mall.entity.Product;
import com.csnf.mall.entity.ProductImage;
import com.csnf.mall.mapper.ProductImageMapper;
import com.csnf.mall.mapper.ProductMapper;
import com.csnf.mall.servermvc.IAdminProductServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
// @Service 指定这个 类是一个 服务型实现 （spring 能够扫描到）
// @Autowired 指定这个 变量 自动实例化(不需要关注实现）
@Service
public class AdminProductServerImp implements IAdminProductServer {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductImageMapper imageMapper;
    @Override
    public List<Product> getAllProducts() {
        return productMapper.getAllProduct();
    }

    @Override
    public List<Product> getProductByParam(String pName, Integer cId) {
        // 处理 cid = 0的情况
        if(cId==0){
            cId = null;
        }
        return productMapper.getProducts(pName, cId);
    }

    @Override
    public Product getProductDetail(Integer productId) {
        Product product = productMapper.getProductById(productId);
        if(null!=product){
            List<ProductImage> images = imageMapper.getImagesByProductId(productId);
            product.setProductImages(images);
        }
        return product;
    }
}
